package com.karolak.stock.oscilator;

import com.karolak.stock.data.DataManager;
import com.karolak.stock.indicator.Indicator;

public class CrossOsc extends Indicator {

	private Indicator crosser;
	
	/**
	 * @param dataManager
	 * @param period
	 * @param input
	 */
	public CrossOsc(DataManager dataManager, int period, Indicator base, Indicator crosser) {
		super(dataManager, period, base);
		this.crosser = crosser;
		setName(String.format("%s%s%s", super.getName(), getInput().getName(), getCrosser().getName()));
	}

	public double execute(double x) {
		double base = getDataManager().getLast(getInput().getName());
		double base1 = getDataManager().getLast(getInput().getName(), 1);
		double crosser = getDataManager().getLast(getCrosser().getName());
		double crosser1 = getDataManager().getLast(getCrosser().getName(), 1);
		
		if (crosser1 <= base1 && base < crosser) {
			return 1;
		}

		if (crosser1 >= base1 && base > crosser) {
			return -1;
		}
		return 0;
	}

	protected Indicator getCrosser() {
		return crosser;
	}
}

